﻿//Author:Michal Sporna
//License: https://operationsneakpeek.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Util.Classes
{
    /// <summary>
    /// Item in the world
    /// </summary>
    public class Item
    {
        #region properties
        public string Name;
        public double Width;
        public double Height;
        public double Offset;
        public string TextureName;
        public double Efficiency;
        public bool VisibleInInventory;
        public Util.Helpers.PublicEnums.ItemAction ActionType;
        public bool HasState2;
        public string ItemState2;

        public List<Util.Classes.Event> Event;
        

        #endregion

        #region helpers
        public string WidthString;
        public string HeightString;
        public string OffsetString;
        public string Type;
        public string ActionTypeString;
        public double AnimationStartTime; //needed when animating the item
        public bool ThrowInProgress;

        public Util.Helpers.PublicEnums.ItemMoveDirection ThrowDirection;
        #endregion

        #region methods

       

        /// <summary>
        /// Convert all values from temporary string
        /// to types
        /// </summary>
        public void DoConvertions()
        {
            try
            {
                

                #region action type

                switch (this.ActionTypeString)
                {
                    case "None":
                        {
                            this.ActionType = Helpers.PublicEnums.ItemAction.None;
                            break;
                        }
                    case "Active":
                        {
                            this.ActionType = Helpers.PublicEnums.ItemAction.Active;
                            break;
                        }
                    case "Passive":
                        {
                            this.ActionType = Helpers.PublicEnums.ItemAction.Passive;
                            break;
                        }
                    case "Interaction":
                        {
                            this.ActionType = Helpers.PublicEnums.ItemAction.Interaction;
                            break;
                        }
                }

                #endregion

                #region parameters of the events

                for (int i = 0; i < this.Event.Count; i++)
                {
                    //Event
                    #region event type

                    switch (this.Event[i].EventTypeString)
                    {
                        case "NONE":
                            {
                                this.Event[i].EventType = Util.Helpers.PublicEnums.EventType.None;
                                this.Event[i].EventTypeString = null;
                                break;
                            }
                        case "onItemCollected":
                            {
                                this.Event[i].EventType = Util.Helpers.PublicEnums.EventType.onItemCollected;
                                this.Event[i].EventTypeString = null;
                                break;
                            }
                        case "onItemUsed":
                            {
                                this.Event[i].EventType = Util.Helpers.PublicEnums.EventType.onItemUsed;
                                this.Event[i].EventTypeString = null;
                                break;
                            }

                    }
                }


                    #endregion


                //    //fpreach param
                //    for (int i1 = 0; i1 < this.Events[i].Parameters.Count; i1++)
                //    {
                //        switch (this.Events[i].Parameters[i1].ParameterString)
                //        {
                //            case "this":
                //                {
                //                    this.Events[i].Parameters[i1].Parameter = this;
                //                    //this.Events[i].Parameters[i1].ParameterString = null;

                //                    break;
                //                }
                //            case "NULL":
                //                {
                //                    this.Events[i].Parameters[i1].Parameter = null;
                //                    //this.Events[i].Parameters[i1].ParameterString = null;

                //                    break;
                //                }

                //        }
                //    }
                //}


                #endregion
            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }


        #region moving item



       
        #endregion

        /// <summary>
        /// Use the item
        /// </summary>
        public void UseItem()
        {
            try
            {
                //usage of an item differs based on action type
                switch (ActionType)
                {
                    case Helpers.PublicEnums.ItemAction.Active:
                        {
                            //raise event
                            #region raise on item used event
                            for (int i = 0; i < this.Event.Count; i++)
                            {
                                if (this.Event[i].EventType == Helpers.PublicEnums.EventType.onItemUsed)
                                {
                                    //assign parameters
                                    this.Event[i].Parameters[0].Parameter = this;
                               

                                    //raise event
                                    World.EventHandler.RaiseEvent(this.Event[i]);
                                    break;


                                }
                            }


                            #endregion

                            break;
                        }
                    case Helpers.PublicEnums.ItemAction.Interaction:
                        {
                            //show infromation that this can be used only in interaction with other item/obstacle

                            break;
                        }
                    case Helpers.PublicEnums.ItemAction.Passive:
                        {
                            //raise event
                            #region raise on item used event
                            for (int i = 0; i < this.Event.Count; i++)
                            {
                                if (this.Event[i].EventType == Helpers.PublicEnums.EventType.onItemUsed)
                                {
                                    //assign parameters
                                    this.Event[i].Parameters[0].Parameter = this;


                                    //raise event
                                    World.EventHandler.RaiseEvent(this.Event[i]);
                                    break;


                                }
                            }


                            #endregion

                            break;
                        }
                }






            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
                
            }

        }

        /// <summary>
        /// throw item
        /// </summary>
        public void ThrowItem()
        {
            try
            {

                if (!World.WorldManager.ThrowDirectionChosen)
                {
                    //first display arrows for user, to determine in which direction the throw is going to be made
                    Util.Cache.Avatar.DisposeThrowArrows(); //first dispose to init all private fields 
                    Util.Cache.Avatar.ShowThrowArrows();
                    World.WorldManager.ThrownItem = this;
                }
                else
                {

                    //create throw reach rect
                    //World.WorldManager.ThrownItem = this;
                    //World.WorldManager.FieldOfThrowReachRect = new Rectangle((int)((Util.Cache.Avatar.CurrentPosition.X+(Util.Cache.Avatar.Width/2)) - ((this.Efficiency * 10) / 2)), (int)(Util.Cache.Avatar.CurrentPosition.Y - ((this.Efficiency * 10) / 2)), (int)this.Efficiency * 10 + Util.Cache.Avatar.Width, (int)this.Efficiency * 10 + Util.Cache.Avatar.Height);

                    #region prepare field of throw, based on throw direction chosen

                    switch (this.ThrowDirection)
                    {
                        case Helpers.PublicEnums.ItemMoveDirection.Up:
                            {
                                World.WorldManager.FieldOfThrowReachRect = new Rectangle((int)((Util.Cache.Avatar.CurrentPosition.X + (Util.Cache.Avatar.Width / 2)) - ((this.Efficiency * 10) / 2)), (int)((Util.Cache.Avatar.CurrentPosition.Y+Util.Cache.Avatar.Height) - (this.Efficiency * 20)), (int)this.Efficiency * 10, (int)this.Efficiency * 20);

                                break;
                            }
                        case Helpers.PublicEnums.ItemMoveDirection.Down:
                            {
                                World.WorldManager.FieldOfThrowReachRect = new Rectangle((int)((Util.Cache.Avatar.CurrentPosition.X + (Util.Cache.Avatar.Width / 2)) - ((this.Efficiency * 10) / 2)), (int)Util.Cache.Avatar.CurrentPosition.Y, (int)this.Efficiency * 10, (int)this.Efficiency * 20);
                                break;
                            }
                        case Helpers.PublicEnums.ItemMoveDirection.Left:
                            {
                                World.WorldManager.FieldOfThrowReachRect = new Rectangle((int)((Util.Cache.Avatar.CurrentPosition.X + Util.Cache.Avatar.Width) - (this.Efficiency * 20)), (int)((Util.Cache.Avatar.CurrentPosition.Y + (Util.Cache.Avatar.Height/2)) - (this.Efficiency * 10)/2), (int)this.Efficiency * 20, (int)this.Efficiency * 10);
                                break;
                            }
                        case Helpers.PublicEnums.ItemMoveDirection.Right:
                            {
                                World.WorldManager.FieldOfThrowReachRect = new Rectangle((int)(Util.Cache.Avatar.CurrentPosition.X), (int)((Util.Cache.Avatar.CurrentPosition.Y + (Util.Cache.Avatar.Height / 2)) - (this.Efficiency * 10) / 2), (int)this.Efficiency * 20, (int)this.Efficiency * 10);
                                break;
                            }


                    }


                    #endregion


                    World.WorldManager.ThrowPosition = new Vector2(-1, -1);//reset throw position
                    World.WorldManager.NPCToDistract = new List<Characters.NPC>();
                    World.WorldManager.DrawThrowFieldOfReachFlag = true;
                    World.WorldManager.ThrowFieldOfReachReady = false;
                    World.WorldManager.ThrowComplete = false;
                    World.WorldManager.CurrentElapsedTimeUponThrowComplete = -1;
                    World.WorldManager.StoredFOVs = new List<FieldOfViewBackup>();
                    this.ThrowInProgress = false;
                    this.AnimationStartTime = Util.Cache.GameTime.TotalGameTime.TotalMilliseconds;//save current time elapsed
                }

            }
            catch (Exception ex)
            {
                Util.ErrorLogging.LogError(ex);
                return;
            }
        }

        #endregion
    }
}
